package io.renren.modules.carinsurance.controller;

import io.renren.common.utils.PageData;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.carinsurance.dto.CarInsuranceDTO;
import io.renren.modules.carinsurance.service.CarInsuranceService;
import io.renren.modules.carinsurance.vo.CarInsuranceVO;
import io.renren.modules.carinsurance.vo.DealInsuranceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 车辆保险类型
 *
 */
@Api(tags = "车辆保险类型")
@RestController
@RequestMapping("/carinsurance")
public class CarInsuranceController {

    @Autowired
    private CarInsuranceService carInsuranceService;

    @GetMapping("/list")
    @ApiOperation("车辆保险类型信息(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = "carInsuranceType", value = "车辆保险类型", paramType = "query", dataType="String")
    })
    public R list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CarInsuranceVO> pageData = carInsuranceService.queryInsurancePage(params);
        return R.ok().put("page", pageData);
    }

    @ApiOperation("新增车辆保险类型")
    @PostMapping
    public R save(@RequestBody CarInsuranceDTO dto){
        // 验证参数
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        carInsuranceService.saveCarInsurance(dto);
        return R.ok();
    }

    @ApiOperation("修改车辆保险类型")
    @PutMapping
    public R update(@RequestBody CarInsuranceDTO dto){
        // 验证参数
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        carInsuranceService.editById(dto);
        return R.ok();
    }

    @ApiOperation("删除车辆保险类型")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        carInsuranceService.deleteByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/getinsurancename")
    @ApiOperation("获取车辆保险类型")
    public R getInsuranceName(){
        List<DealInsuranceVO> dealInsuranceVOList = carInsuranceService.getInsuranceName();
        return R.ok().put("dealInsuranceVOList", dealInsuranceVOList);
    }
}
